package gold.digger;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC777 {
    public long startExecuteTime = System.currentTimeMillis();


    /*
     * @param 此题目参考了别人代码
     * 这是因为问题情况较为复杂
     * 未来需要再次复习此道题目
     *  经典智力题：双指针
     * @return:
     */
    class Solution {
        public boolean canTransform(String startStr, String endStr) {
            // The key is L can only move left while R can only move right
            if (startStr.length() != endStr.length()) return false;
            char[] start = startStr.toCharArray(), end = endStr.toCharArray();

            int N = start.length;
            int i = 0;
            int j = 0;
            while (i < N && j < N) {
                while (i < N && start[i] == 'X') {
                    i++;
                }
                while (j < N && end[j] == 'X') {
                    j++;
                }

                // handle out of boundary case
                if (i == N && j == N) {
                    return true;
                } else if (i == N || j == N) {
                    return false;
                }


                if (start[i] != end[j]) {
                    return false;
                }

                if (i < N && start[i] == 'L' && i < j) {
                    return false;
                }
                if (i < N && start[i] == 'R' && i > j) {
                    return false;
                }
                // matched once
                i++;
                j++;
            }

            while (i < N) {
                if (start[i] != 'X') {
                    return false;
                }
                i++;
            }

            while (j < N) {
                if (end[j] != 'X') {
                    return false;
                }
                j++;
            }

            return true;
        }
    }

    public void run() {
        Solution solution = new Solution();
        List<Integer> list = new ArrayList<>();
        System.out.println(solution.toString());
    }

    public static void main(String[] args) throws Exception {
        LC777 an = new LC777();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
